package net.lzzy.practices.web.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lzzy.practices.constants.ApiConstants;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Option;
import net.lzzy.practices.entities.Question;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.services.QuestionService;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Api(tags = "题目相关业务")
@CrossOrigin
@RestController
@RequestMapping(ApiConstants.ROUTE_QUESTION_ROOT)
public class QuestionApiController {
    private final QuestionService service;

    @Autowired
    public QuestionApiController(QuestionService service) {
        this.service = service;
    }

    @ApiOperation(value = "获取所有题目章节", notes = "<b>参数列表：<b/><br/>&nbsp;&nbsp;&nbsp;&nbsp;" +
            "1.chapterId值<br/>&nbsp;&nbsp;&nbsp;&nbsp;2.token值<br/>")
    @GetMapping(ApiConstants.ROUTE_QUESTION_CHAPTER)
    public ApiResult get(@RequestParam(name = "id")UUID chapterId, @RequestParam String token){
        boolean teacher = !AuthUtils.illegalUser(token, new int[]{User.TYPE_TEACHER});
        List<VmQuestion> questions=new ArrayList<>();
        service.get(chapterId).forEach(question -> questions.add(VmQuestion.create(question, teacher)));
        return new ApiResult(true, questions.size()+"",questions);
    }

    private ApiResult create(JSONObject json){
        String token=json.getString(ApiConstants.KEY_TOKEN);
        String content= json.getString(ApiConstants.KEY_QUESTION_CONTENT);
        String analysis= json.getString(ApiConstants.KEY_QUESTION_ANALYSIS);
        Integer ordinal= json.getInteger(ApiConstants.KEY_QUESTION_ORDINAL);
        String strChapterId= json.getString(ApiConstants.KEY_QUESTION_CHAPTER_ID);
        Integer type=json.getInteger(ApiConstants.KEY_QUESTION_TYPE);
        if (StringUtils.isEmpty(token)||
                StringUtils.isEmpty(content)||
                StringUtils.isEmpty(analysis)||
                StringUtils.isEmpty(strChapterId)||
                ordinal==null||type==null){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID chapterId= UUID.fromString(strChapterId);
        Question question= new Question();
        JSONArray array=json.getJSONArray(ApiConstants.KEY_QUESTION_OPTIONS);
        List<Option> options=new ArrayList<>();
        if (array!=null&&array.size()>0){
            for (int i=0; i<array.size();i++){
                JSONObject jo=array.getJSONObject(i);
                Option option =new Option();
                option.setAnswer(jo.getBoolean(ApiConstants.KEY_OPTION_ANSWER));
                String oc=(jo.getString(ApiConstants.KEY_OPTION_CONTENT));
                String ol=(jo.getString(ApiConstants.KEY_OPTION_LABEL));
                if(StringUtils.isEmpty(oc)||StringUtils.isEmpty(ol)){
                    return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
                }
                option.setContent(oc);
                option.setLabel(ol);
                option.setQuestion(question);
                options.add(option);
            }
        }
        question.setOptions(options);
        question.setContent(content);
        question.setAnalysis(analysis);
        question.setOrdinal(ordinal);
        question.setType(type);
        Question result=service.add(chapterId,question,token);
        if (result == null){
            return new ApiResult(false, Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @ApiOperation(value = "添加题目",notes = "1.token(String)<br/>2.content(String)<br/>3.analysis(String)<br/>"+
            "4.ordinal(int)<br/>5.chapterId(String)<br/>6.type(int)<br/>7.options(Array)<br/>&nbsp;&nbsp;[<br/>&nbsp;nbsp;&nbsp;&nbsp;&nbsp;{<br/>"+
            "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7.1content(String)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7.2label(String)<br/>"+
            "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;7.3answer(boolean)<br/>&nbsp;&nbsp;&nbsp;&nbsp;}<br/>&nbsp;&nbsp;]")
    @PostMapping(ApiConstants.ROUTE_QUESTION_ADD)
    public ApiResult add(@RequestBody JSONObject json){
        return create(json);
    }

    @ApiOperation(value = "老师更新课程的章节的题目", notes = "1.token(String)<br/>2.content(String)<br/>3.analysis(String)<br/>" +
            "4.ordinal(int)<br/>5.type(int)<br/>")
    private ApiResult update (UUID id, QuestionputInfo info){
        String token=info.getToken();
        String content= info.getContent();
        String analysis= info.getAnalysis();
        Integer ordinal= info.getOrdinal();
        Integer type= info.getType();
        if (StringUtils.isEmpty(token)||
                StringUtils.isEmpty(content)||
                StringUtils.isEmpty(analysis)||
                ordinal==null||type==null||id==null){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        Question question=new Question();
        question.setContent(content);
        question.setAnalysis(analysis);
        question.setOrdinal(ordinal);
        question.setType(type);
        Question result=service.update(id,question,token);
        if (result==null){
            return new ApiResult(false, Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @ApiOperation(value = "提交题目", notes = "<b>参数值：<b/><br/>1.id值<br/>2.info值<br/>")
    @PostMapping(ApiConstants.ROUTE_QUESTION_PUT)
    public ApiResult put(@PathVariable UUID id,@RequestBody QuestionputInfo info){
        return update(id,info);
    }

    @ApiOperation(value = "为题目添加选项", notes = "<b>参数列表：<b/><br/>1.token(String)<br/>2.strQuestionId(String)<br/>3.content(String)<br/>" +
            "4.label(String)<br/>5.answer(boolean)<br/>")
    @PostMapping(ApiConstants.ROUTE_QUESTION_OPTION_ADD)
    public ApiResult addOption(@RequestBody OptionInfo info){
        String token=info.getToken();
        String strQuestionId= info.getQuestionId();
        String content= info.getContent();
        String label= info.getLabel();
        Boolean answer=info.getAnswer();
        if (StringUtils.isEmpty(token)||
                StringUtils.isEmpty(strQuestionId)||
                StringUtils.isEmpty(content)||
                StringUtils.isEmpty(label)||
                answer==null){
            return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        Option option =new Option();
        option.setContent(content);
        option.setLabel(label);
        option.setAnswer(answer);
        UUID questionId=UUID.fromString(strQuestionId);
        Question question=service.addOption(questionId,option,token);
        if (question==null){
            return new ApiResult(false,Messages.WRONG_ID+"或"+Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @ApiOperation(value = "删除题目的选项", notes = "<b>参数列表：<b/><br/>1.token(String)<br/>2.strId(String)<br/>")
    @PostMapping(ApiConstants.ROUTE_QUESTION_OPTION_DEL)
    public ApiResult removeOption(@RequestBody KeyInfo info){
        String token=info.getToken();
        String strId=info.getId();
        if (StringUtils.isEmpty(token)||StringUtils.isEmpty(strId)){
            return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID id=UUID.fromString(strId);
        Question question=service.removeOption(id,token);
        if (question==null){
            return new ApiResult(false,Messages.WRONG_ID + "或" + Messages.NO_PERMISSION,null);
        }
        return new ApiResult(true, Messages.SUCCESS.toString(), VmQuestion.create(question,true));
    }

    @ApiOperation(value = "老师删除课程章节的题目", notes = "<b>参数列表：<b/><br/>" +
            "1.token(String)<br/>2.strId(String)<br/>")
    @DeleteMapping(ApiConstants.ROUTE_QUESTION_DEL)
    public ApiResult remove(@RequestBody KeyInfo info){
        String token=info.getToken();
        String strId=info.getId();
        if (StringUtils.isEmpty(token)||StringUtils.isEmpty(strId)){
            return new ApiResult(false,Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID id=UUID.fromString(strId);
        Messages msg=service.remove(id,token);
        return new ApiResult(msg==Messages.SUCCESS,msg.toString(),null);
    }
}
